package com.team.mapper;

import com.team.dto.DocumentDto;
import com.team.entity.DocumentReview;
import com.team.vo.DocumentReviewVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DocumentMapper {
    /**
     * 查询所有文档状态
     * @return
     */
    @Select("SELECT dr.did,dr.name, dr.applicant, dr.time, " +
            "CASE " +
            "    WHEN dr.status = 0 THEN CONCAT('已受理：', u.name) " +
            "    WHEN dr.status = 1 THEN CONCAT('审核通过：', u.name) " +
            "    WHEN dr.status = -1 THEN CONCAT('审核驳回：', u.name) " +
            "    ELSE '未审核' " +
            "END AS status " +
            "FROM document_reviews dr " +
            "LEFT JOIN user u ON dr.student_id = u.student_id")
    List<DocumentReviewVo> getAllApprovals();

    /**
     * 文档审核（更新审核人和对应文档状态）
     * @param studentId
     * @param status
     * @param did
     */
    @Update("update document_reviews set status=#{status},student_id=#{studentId} where did=#{did} ")
    void updateDocumentStatus(@Param("studentId") int studentId,@Param("status") int status,@Param("did")int did);

    /**
     * 提交文档
     * @param documentDto
     */
    @Insert("INSERT INTO documents (name, student_id, time, cate, start_time, end_time, format, entrants, format_detail, organizers) " +
            "VALUES (#{name}, #{studentId}, #{time}, #{cate}, #{startTime}, #{endTime}, #{format}, #{entrants}, #{formatDetail}, #{organizers})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertDocument(DocumentDto documentDto);

    /**
     * 提交文档时也要在审核信息中加入（默认为未审核 审核人id为空）
     * @param documentReview
     */
    @Insert("INSERT INTO document_reviews (did, name, applicant, time, status)\n" +
            "    VALUES (#{did}, #{name}, #{applicant}, #{time}, #{status})")
    void insertDocumentApproval(DocumentReview documentReview);

    /**
     * 查询团队的所有文档审核状态
     * @param teamName
     * @return
     */
    @Select("SELECT dr.did,dr.name, dr.applicant, dr.time, " +
            "CASE " +
            "    WHEN dr.status = 0 THEN CONCAT('已受理：', u.name) " +
            "    WHEN dr.status = 1 THEN CONCAT('审核通过：', u.name) " +
            "    WHEN dr.status = -1 THEN CONCAT('审核驳回：', u.name) " +
            "    ELSE '未审核' " +
            "END AS status " +
            "FROM document_reviews dr " +
            "LEFT JOIN user u ON dr.student_id = u.student_id where dr.applicant=#{teamName}")
    List<DocumentReviewVo> getMyApprovals(String teamName);
}
